namespace MicroCloud.CodeGeneration.Entities
{
    /// <summary>
    /// 实体类：代码模板
    /// </summary>
    [Description("代码模板")]
    [Table(nameof(CodeTemplate), Schema = Constants.Schema)]
    public class CodeTemplate : EntityBase<Guid>, ICreatedTime
    {
        /// <summary>
        /// 获取或设置 配置名称
        /// </summary>
        [Required, StringLength(200)]
        public string Name { get; set; }

        /// <summary>
        /// 获取或设置 元数据类型
        /// </summary>
        public MetadataType MetadataType { get; set; }

        /// <summary>
        /// 获取或设置 模板文件，默认内置，也可以由用户自定义加载
        /// </summary>
        [Required, StringLength(500)]
        public string TemplateFile { get; set; }

        /// <summary>
        /// 获取或设置 排序
        /// </summary>
        public int Order { get; set; }

        /// <summary>
        /// 获取或设置 代码输出文件名格式
        /// </summary>
        [Required, StringLength(300)]
        public string OutputFileFormat { get; set; }

        /// <summary>
        /// 获取或设置 是否只生成一次
        /// </summary>
        public bool IsOnce { get; set; }

        /// <summary>
        /// 获取或设置 系统类型
        /// </summary>
        public bool IsSystem { get; set; }

        /// <summary>获取或设置 创建时间</summary>
        public DateTime CreatedTime { get; set; }

        /// <summary>
        /// 获取或设置 项目模板集合
        /// </summary>
        public virtual ICollection<CodeProjectTemplate> ProjectTemplates { get; set; } = new List<CodeProjectTemplate>();

        public Type GetInnerTemplateType()
        {
            switch (Name)
            {
                case "cs_Entity":
                    return typeof(cs_Entity);
                case "cs_EntityConfigurationBase":
                    return typeof(cs_EntityConfigurationBase);
                case "cs_EntityConfiguration":
                    return typeof(cs_EntityConfiguration);

                case "cs_InputDto":
                    return typeof(cs_InputDto);
                case "cs_OutputDto":
                    return typeof(cs_OutputDto);

                case "cs_AutoMapperConfiguration":
                    return typeof(cs_AutoMapperConfiguration);
                case "cs_EntityStore":
                    return typeof(cs_EntityStore);

                case "cs_ModuleContract":
                    return typeof(cs_ModuleContract);
                case "cs_ModuleService":
                    return typeof(cs_ModuleService);
                case "cs_ModulePack":
                    return typeof(cs_ModulePack);

                case "cs_ControllerBase":
                    return typeof(cs_ControllerBase);
                case "cs_Controller":
                    return typeof(cs_Controller);

                default:
                    return null;
            }
        }

        /// <summary>
        /// 获取项目代码输出文件名
        /// </summary>
        public string GetCodeFileName(CodeProject project)
        {
            var projectCode = project.GetCode();
            string fileName = OutputFileFormat.Replace("{Project.NamespacePrefix}", project.NamespacePrefix)
                .Replace("{Project.Name:Lower}", projectCode.ToLowerCase())
                .Replace("{Project.Name}", projectCode);
            return fileName;
        }

        /// <summary>
        /// 获取项目代码输出文件名
        /// </summary>
        public string GetCodeFileName(CodeModule module)
        {
            string fileName = OutputFileFormat.Replace("{Project.NamespacePrefix}", module.Project.NamespacePrefix)
                .Replace("{Module.Name:Lower}", module.Name.ToLowerCase())
                .Replace("{Module.Name}", module.Name);
            return fileName;
        }

        /// <summary>
        /// 获取项目代码输出文件名
        /// </summary>
        public string GetCodeFileName(CodeEntity entity)
        {
            string fileName = OutputFileFormat.Replace("{Project.NamespacePrefix}", entity.Module.Project.NamespacePrefix)
                .Replace("{Module.Name:Lower}", entity.Module.Name.ToLowerCase())
                .Replace("{Entity.Name:Lower}", entity.Name.ToLowerCase())
                .Replace("{Module.Name}", entity.Module.Name).Replace("{Entity.Name}", entity.Name);
            return fileName;
        }

    }
}
